package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.shop.mapper.ErpSellerStorePromotionActivityProductMapper;
import com.erp.shop.service.IErpSellerStorePromotionActivityProductService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.shop.api.domain.ErpSellerStorePromotionActivityProduct;
import com.ruoyi.shop.api.domain.bo.ErpSellerStorePromotionActivityProductBo;
import com.ruoyi.shop.api.domain.vo.ErpSellerStorePromotionActivityProductVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商家促销活动商品Service业务层处理
 *
 * @author erp
 * @date 2023-06-08
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStorePromotionActivityProductServiceImpl implements IErpSellerStorePromotionActivityProductService {

    private final ErpSellerStorePromotionActivityProductMapper baseMapper;

    /**
     * 查询商家促销活动商品
     */
    @Override
    public ErpSellerStorePromotionActivityProductVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商家促销活动商品列表
     */
    @Override
    public TableDataInfo<ErpSellerStorePromotionActivityProductVo> queryPageList(ErpSellerStorePromotionActivityProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStorePromotionActivityProduct> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStorePromotionActivityProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商家促销活动商品列表
     */
    @Override
    public List<ErpSellerStorePromotionActivityProductVo> queryList(ErpSellerStorePromotionActivityProductBo bo) {
        LambdaQueryWrapper<ErpSellerStorePromotionActivityProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStorePromotionActivityProduct> buildQueryWrapper(ErpSellerStorePromotionActivityProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStorePromotionActivityProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoreProductId() != null, ErpSellerStorePromotionActivityProduct::getStoreProductId, bo.getStoreProductId());
        lqw.eq(bo.getAeProductId() != null, ErpSellerStorePromotionActivityProduct::getAeProductId, bo.getAeProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuInventoryList()), ErpSellerStorePromotionActivityProduct::getSkuInventoryList, bo.getSkuInventoryList());
        lqw.eq(StringUtils.isNotBlank(bo.getProductDiscountList()), ErpSellerStorePromotionActivityProduct::getProductDiscountList, bo.getProductDiscountList());
        lqw.eq(bo.getPromotionId() != null, ErpSellerStorePromotionActivityProduct::getPromotionId, bo.getPromotionId());
        lqw.eq(bo.getActivityId() != null, ErpSellerStorePromotionActivityProduct::getActivityId, bo.getActivityId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreClubDiscountRate()), ErpSellerStorePromotionActivityProduct::getStoreClubDiscountRate, bo.getStoreClubDiscountRate());
        lqw.eq(StringUtils.isNotBlank(bo.getExistStoreFansDiscount()), ErpSellerStorePromotionActivityProduct::getExistStoreFansDiscount, bo.getExistStoreFansDiscount());
        lqw.eq(bo.getBuyMaxNum() != null, ErpSellerStorePromotionActivityProduct::getBuyMaxNum, bo.getBuyMaxNum());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStorePromotionActivityProduct::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增商家促销活动商品
     */
    @Override
    public Boolean insertByBo(ErpSellerStorePromotionActivityProductBo bo) {
        ErpSellerStorePromotionActivityProduct add = BeanUtil.toBean(bo, ErpSellerStorePromotionActivityProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商家促销活动商品
     */
    @Override
    public Boolean updateByBo(ErpSellerStorePromotionActivityProductBo bo) {
        ErpSellerStorePromotionActivityProduct update = BeanUtil.toBean(bo, ErpSellerStorePromotionActivityProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStorePromotionActivityProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商家促销活动商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
